package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.HotelAvailabilityMgr;
import ejb.contentManagement.HotelMgr;
import ejb.dto.HotelAvailabilityDTO;
import ejb.dto.HotelDTO;


@ManagedBean
@RequestScoped
public class HotelAvailabilityBean {
	
	public HotelAvailabilityBean() {
		hotelAvailability = new HotelAvailabilityDTO();
		hotelAvailability.setHotelId(hotelId);
		setErrorMessage(null);
	}
	
	@EJB
	private HotelAvailabilityMgr hotelAvailabilityMgr; 
	
	@EJB
	private HotelMgr hotelMgr;
	
	@SuppressWarnings("unused")
	private List<HotelAvailabilityDTO> allHotelAvailabilities;
	
	private int hotelId;
	private int hotelAvailabilityId;
	@SuppressWarnings("unused")
	private HotelDTO hotel;
	private HotelAvailabilityDTO hotelAvailability;
	private String errorMessage;
	
	/**
	 * Delete HotelAvailability from database
	 * @param id
	 * @return redirect to page <code>manageHotelAvailability</code>
	 */
	public String deleteHotelAvailability(int id) {
		System.out.println("Yeah");
		boolean result = hotelAvailabilityMgr.deleteHotelAvailability(id);
		System.out.println(result);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageHotelAvailability?faces-redirect=true&hotelId=" + hotelId;
	}
	
	/**
	 * Update HotelAvailability in database
	 * @return redirect to page <code>manageHotelAvailability</code>
	 */
	public String updateHotelAvailability() {
		hotelAvailability.setHotelId(hotelId);
		boolean result = hotelAvailabilityMgr.updateHotelAvailability(hotelAvailability);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageHotelAvailability?faces-redirect=true&hotelId=" + hotelId;
	}
	
	/**
	 * Insert HotelAvailability in database
	 * @return redirect to page <code>manageHotelAvailability</code>
	 */
	public String insertHotelAvailability() {
		hotelAvailability.setHotelId(hotelId);
		boolean result = hotelAvailabilityMgr.insertHotelAvailability(hotelAvailability);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageHotelAvailability?faces-redirect=true&hotelId=" + hotelId;
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<HotelAvailabilityDTO> getAllHotelAvailabilities() {
		return hotelAvailabilityMgr.getAllHotelAvailabilitiesDTO(hotelId);
	}

	public void setAllHotelAvailabilities(List<HotelAvailabilityDTO> allHotelAvailabilities) {
		this.allHotelAvailabilities = allHotelAvailabilities;
	}


	public int getHotelId() {
		return hotelId;
	}

	public void setHotelId(int hotelId) {
		this.hotelId = hotelId;
	}

	public HotelDTO getHotel() {
		return hotelMgr.getHotelDTO(hotelId);
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}
	
	public HotelAvailabilityDTO getHotelAvailability(){
		return this.hotelAvailability;
	}
	
	public void setHotelAvailability(HotelAvailabilityDTO hotelAvailability){
		this.hotelAvailability = hotelAvailability;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getHotelAvailabilityId() {
		return hotelAvailabilityId;
	}

	public void setHotelAvailabilityId(int hotelAvailabilityId) {
		this.hotelAvailabilityId = hotelAvailabilityId;
		hotelAvailability = hotelAvailabilityMgr.getHotelAvailabilityDTO(hotelAvailabilityId);
		if(hotelAvailability == null)
			SessionBean.notFound();
	}	

	
}
